/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-02-20 16:38:49
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-07-13 15:52:34
 */

'use strict';

define([
    './baseBean',
], function(
    BaseBean
) {
    function me() {
        BaseBean.apply(this, arguments);
        var self = this;
        this.getAccounts = function(params,callback, errorCallback) {
            self.p('/admanage/getAccounts', params, function(result) {
                if (callback) {
                    callback(result, params);
                }
            }, errorCallback);
        }
        this.groupbyadset = function(params, callback, errorCallback) {
            self.p('/admanage/groupbyadset', params, function(result) {
                if (callback) {
                    var d = self._convertGroupbyadsetData(result);
                    d._params = params; //将查询参数设置结果集
                    callback(d, params);
                }
            }, errorCallback);
        }

        this.updateAd = function(params, callback, errorCallback) {
            self.p('/admanage/updateAd', params, function(result) {
                if (callback) {
                    callback(result, params);
                }
            }, errorCallback);
        }

        this.getAdTitleHistory = function(params, callback, errorCallback) {
            self.p('/cache/getAdTitleHistory', params, function(result) {
                if (callback) {
                    callback(result, params);
                }
            }, errorCallback);
        }

        this.getJob = function(params, callback, errorCallback) {
            self.post('/admanage/getJob', params, function(result) {
                if (callback) {
                    callback(result, params);
                }
            }, errorCallback);
        }

        this.getAdSet = function(params, callback, errorCallback) {
            self.p('/admanage/getAdSet', params, function(result) {
                if (callback) {
                    callback(result, params);
                }
            }, errorCallback);
        }

        this.getAd = function(params, callback, errorCallback) {
            self.p('/admanage/getAd', params, function(result) {
                if (callback) {
                    callback(result, params);
                }
            }, errorCallback);
        }

        this.updateAdSet = function(params, callback, errorCallback) {
            self.p('/admanage/updateAdSet', params, function(result) {
                if (callback) {
                    callback(result, params);
                }
            }, errorCallback);
        }

        /**
         * { 将数据转为需要的格式 }
         *
         * @method     _convertGroupbyadsetData
         * @param      {<type>}  result  { description }
         */
        this._convertGroupbyadsetData = function(result) {
            var newData = [];
            var statusLabel = {
                ACTIVE: I18N.getText('management', 'active'),
                PAUSED: I18N.getText('management', 'paused'),
                DELETED: I18N.getText('management', 'deleted'),
                PENDING_REVIEW: I18N.getText('management', 'pendingReview'),
                DISAPPROVED: I18N.getText('management', 'disapproved'),
                PREAPPROVED: I18N.getText('management', 'preapproved'),
                PENDING_BILLING_INFO: I18N.getText('management', 'pendingBillingInfo'),
                CAMPAIGN_PAUSED: I18N.getText('management', 'campaignPaused'),
                ARCHIVED: I18N.getText('management', 'archived'),
                ADSET_PAUSED: I18N.getText('management', 'adsetPaused')
            }

            _.each(result.adSets, function(adset) {
                var tempArray = [{ status: (adset.configuredStatus == 'ACTIVE'), value: adset.configuredStatus }];
                var adsetLabel = self.filterTableData(statusLabel,adset,tempArray);
                //    [
                //    { status: (adset.configuredStatus == 'ACTIVE'), value: adset.configuredStatus }, //广告状态
                //    { name: adset.name + '', value: adset.name }, //名称
                //    { name: adset.campaignName + '', value: adset.campaignName }, //campaignName
                //    { text: statusLabel[adset.status], value: adset.status }, //状态
                //    { text: self._formatMoney(adset.todaySpent), value: adset.todaySpent }, //今日花费
                //    { text: self._formatMoney(adset.spent), value: adset.spent }, //总花费
                //    { text: adset.clicks + '', value: adset.clicks }, //点击
                //    { text: self._formatPercent(adset.ctr), value: adset.ctr }, //点击率
                //    { text: self._formatMoney(adset.cpc), value: adset.cpc }, //CPC
                //    { text: adset.purchase + '', value: adset.purchase }, //Purchase
                //    { text: self._formatPercent(adset.purchaseR), value: adset.purchaseR }, //PurchaseR
                //    { text: self._formatMoney(adset.cps), value: adset.cps }, //CPA
                //    { text: self._formatMoney(adset.salesAmount), value: adset.salesAmount }, //SalesAmount
                //    { text: adset.roi.toFixed(2) + '', value: adset.roi.toFixed(2) }, //ROI
                //    { text: adset.cart+ '', value: adset.cart }, //总花费
                //    { text: self._formatMoney(adset.budget), value: adset.budget }, //预算
                //    { text: adset.bidAmount==0?'-':self._formatMoney(adset.bidAmount), value: adset.bidAmount }, //出价
                //    { text: self._formatPercent(adset.cartR), value: adset.cartR }, //总花费
                //    { text: adset.gaClicks+ '', value: adset.gaClicks }, //总花费
                //    { text: adset.gaCart+ '', value: adset.gaCart }, //总花费
                //    { text: adset.gaPays+ '', value: adset.gaPays }, //总花费
                //    { text: self._formatMoney(adset.gaOrderAmount), value: adset.gaOrderAmount }, //总花费
                //    { text: self._formatPercent(adset.gaCartR), value: adset.gaCartR }, //总花费
                //    { text: self._formatMoney(adset.gaCpa), value: adset.gaCpa }, //总花费
                //    { text: adset.gaCpc.toFixed(2), value: adset.gaCpc.toFixed(2) }, //总花费
                //    { text: self._formatMoney(adset.gaOrderPrice), value: adset.gaOrderPrice }, //总花费
                //    { text: adset.gaRoi.toFixed(2), value: adset.gaRoi.toFixed(2) }, //总花费
                //    { text: self._formatPercent(adset.gaCr), value: adset.gaCr }, //总花费
                //    { text: self._formatLongTime(adset.createdUtcTime), value: adset.createdUtcTime } //创建时间
                //    // { text: adset.clicks + '' ,value:adset.clicks}, //点击量
                //];

                var sub = [];
                _.each(adset.ads, function(ad) {
                    var tempArray = [{ status: (ad.configuredStatus == 'ACTIVE'), value: ad.configuredStatus }];
                    var adLabel =self.filterTableData(statusLabel,ad,tempArray);
                    //    [
                    //    { status: (ad.configuredStatus == 'ACTIVE'), value: ad.configuredStatus }, //广告状态
                    //    { name: ad.name + '', value: ad.name }, //名称
                    //    { name: ad.campaignName + '', value: ad.campaignName }, //campaignName
                    //    { text: statusLabel[ad.status], value: ad.status }, //状态
                    //    { text: self._formatMoney(ad.todaySpent), value: ad.todaySpent }, //今日花费
                    //    { text: self._formatMoney(ad.spent), value: ad.spent }, //总花费
                    //    { text: ad.clicks + '', value: ad.clicks }, //点击
                    //    { text: self._formatPercent(ad.ctr), value: ad.ctr }, //点击率
                    //    { text: self._formatMoney(ad.cpc), value: ad.cpc }, //CPC
                    //    { text: ad.purchase + '', value: ad.purchase }, //Purchase
                    //    { text: self._formatPercent(ad.purchaseR), value: ad.purchaseR }, //PurchaseR
                    //    { text: self._formatMoney(ad.cps), value: ad.cps }, //CPA
                    //    { text: self._formatMoney(ad.salesAmount), value: ad.salesAmount }, //SalesAmount
                    //    { text: ad.roi.toFixed(2) + '', value: ad.roi.toFixed(2) }, //ROI
                    //    { text: ad.cart+ '', value: ad.cart }, //总花费
                    //    { text: self._formatMoney(ad.budget), value: ad.budget }, //预算
                    //    { text: self._formatMoney(ad.bidAmount), value: ad.bidAmount }, //出价
                    //    { text: self._formatPercent(ad.cartR), value: ad.cartR }, //总花费
                    //    { text: ad.gaClicks+ '', value: ad.gaClicks }, //总花费
                    //    { text: ad.gaCart+ '', value: ad.gaCart }, //总花费
                    //    { text: ad.gaPays+ '', value: ad.gaPays }, //总花费
                    //    { text: self._formatMoney(ad.gaOrderAmount), value: ad.gaOrderAmount }, //总花费
                    //    { text: self._formatPercent(ad.gaCartR), value: ad.gaCartR }, //总花费
                    //    { text: self._formatMoney(ad.gaCpa), value: ad.gaCpa }, //总花费
                    //    { text: ad.gaCpc.toFixed(2), value: ad.gaCpc.toFixed(2) }, //总花费
                    //    { text: self._formatMoney(ad.gaOrderPrice), value: ad.gaOrderPrice }, //总花费
                    //    { text: ad.gaRoi.toFixed(2), value: ad.gaRoi.toFixed(2) }, //总花费
                    //    { text: self._formatPercent(ad.gaCr), value: adset.gaCr }, //总花费
                    //    { text: self._formatLongTime(ad.createdUtcTime), value: ad.createdUtcTime } //创建时间
                    //    // { text: ad.clicks + '',value:ad.clicks }, //点击量
                    //];
                    sub.push({
                        id: ('ad-' + ad.id),
                        label: adLabel
                    });
                });

                newData.push({
                    label: adsetLabel,
                    sub: sub,
                    id: ('adset-' + adset.id)
                });
            });
            var res = {
                newData: newData,
                refreshDatetime: result.refreshDatetime,
                summary: result.summary,
            }
            return res;
        }

        this.filterTableData = function(statusLabel,obj,_array){
            var tableHeader = window.localStorage.getItem("tableHeader").split(",");
            var tempObj = {};
            $.each(tableHeader,function(i){
                if(tableHeader[i] == "status"){
                    tempObj = {
                        text:statusLabel[obj[tableHeader[i]]],value:obj[tableHeader[i]]
                    }
                }else if(-1 != $.inArray(tableHeader[i], ["todaySpent","spent","cpc","cps","salesAmount","budget","gaOrderAmount","gaCpa","gaOrderPrice"])){
                    //_formatMoney
                    tempObj = {
                        text:self._formatMoney(obj[tableHeader[i]])+'',value:obj[tableHeader[i]]
                    };
                }else if(-1 != $.inArray(tableHeader[i], ["ctr","purchaseR","cartR","gaCartR","gaCr"])){
                    //_formatMone
                    tempObj = {
                        text:self._formatPercent(obj[tableHeader[i]]),value:obj[tableHeader[i]]
                    };
                }else if(-1 != $.inArray(tableHeader[i], ["roi","gaCpc","gaRoi"])){
                    //_formatMoney
                    tempObj = {
                        text:obj[tableHeader[i]].toFixed(2),value:obj[tableHeader[i]]
                    };
                }else if(-1 != $.inArray(tableHeader[i], ["price"])){  //出价
                    tempObj = {
                        text: obj['bidAmount']==undefined?'-':self._formatMoney(obj['bidAmount']), value: obj['bidAmount']
                    };
                }else if(-1 != $.inArray(tableHeader[i], ["createdUtcTime"])){  //createdUtcTime
                    tempObj = {
                        text: self._formatLongTime(obj['createdUtcTime']), value: obj['createdUtcTime']
                    };
                }else{
                    tempObj = {
                        text:obj[tableHeader[i]]+'',value:obj[tableHeader[i]]
                    };
                }
                _array.push(tempObj);
            });
            return _array;
        }

        /**
         * { 格式金钱 }
         *
         * @method     _formatMoney
         * @param      {<type>}  argument  { description }
         */
        this._formatMoney = function(item) {
            return ((item || item == 0) ? (item == '-' ? '-' : '$ ' + kit.cent_to_dollar(item)) : '-') + '';
        }

        this._formatPercent = function(item) {
            return ((item || item == 0) ? (item == '-' ? '-' : kit.percentage(item)) : '-') + '';
        }

        /**
         * { function_description }
         *
         * @method     _formatLongTime
         * @param      {(number|string)}  item    { description }
         * @return     {<type>}           { description_of_the_return_value }
         */
        this._formatLongTime = function(item) {
            return ((item || item == 0) ? (item == '-' ? '-' : kit.formatOffsetTime(item, window.sessionStorage.timezoneValue)) : '-') + '';
        }


    };
    return new me();
});
